<?php
class user_model extends CI_Model{
	
	function register_user($username, $password, $fname, $lname, $email, $phone, $location, $company, $roleString, $interestString, $activation_code, $dob)
	{
		//input user into user
		
		//encrypted password
		$sha1_password = sha1($password);
		
		$query_str = "INSERT INTO user (user_username, user_password, user_fname, user_lname, user_email, user_phone, user_location, user_company, user_role, user_interests, user_activation_code, user_dob) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
		
		$this->db->query($query_str, array($username, $sha1_password, $fname, $lname, $email, $phone, $location, $company, $roleString, $interestString, $activation_code, $dob));
		
	}
	
	function check_username($username)
	{
		//username checking
		$query_str = 'SELECT user_username from user where user_username = ?';
		
		$result = $this->db->query($query_str, $username);
		
		if ($result->num_rows() > 0)
		{
			//exist
			return true;
		}
		else 
		{
			return false;
		}
	}
	
	function check_email($email)
	{
		//email checking 
		$query_str = 'SELECT user_email from user where user_email = ?';
		
		$result = $this->db->query($query_str, $email);
		
		if ($result->num_rows() > 0)
		{
			//exist
			return true;
		}
		else 
		{
			return false;
		}
	}
	
	
	
	function confirm_registration($registration_code)
	{
		//registration confirmation update if user click the confirmation code on email
		$query_str = "SELECT user_id from user where user_activation_code =?";
		$result = $this->db->query($query_str, $registration_code);
		
		if($result->num_rows() == 1)
		{
			$query_str = "UPDATE user SET user_isactivated = 1 WHERE user_activation_code = ?";
			$result = $this->db->query($query_str, $registration_code);
			
			return true;
		}
		else
		{
			return false;
		}
		
	}
	
	function check_login($username, $password)
	{
		//login check to database 
		
		$sha1_password = sha1($password);
		
		$query_str = "SELECT * FROM user WHERE user_username = ? and user_password = ?";
		
		$result = $this->db->query($query_str, array($username, $sha1_password))->result();
		
		if (count($result) == 1)
		{
			$user = $result[0];
			if($user->user_isactivated == 1)
			{
				return 0;
			}
			else
			{
				//activate id 
				return 1;	
			}
			
		}
		else 
		{
			//wrong password 
			return 2;
		}
	
	}
	
	function search($userString) {
		/*
		 * This function search the database based on the searchString
		 */
		$userResult = array();
		$resultIds = array();
		//search for exact match
		$this -> db -> where('user_username', $userString);
		$this -> db -> where('user_type !=', 'admin');
		$results = $this -> db -> get('user') -> result();
		foreach ($results as $result) {
			$userResult[] = $result;
			$resultIds[] = $result -> user_id;
		}
		//search with like for title and content
		//Where (idea_title LIKE %$searchString% OR idea_overview LIKE %$searchString% OR idea_problem LIKE %$searchString% OR idea_solution LIKE %$searchString%)
		$this -> db -> where('user_type !=', 'admin');
		$this->db->like('user_fname', $userString);
		if (count($resultIds) > 0) {
			$this -> db -> where_not_in('user_id', $resultIds);
		}
		$results = $this -> db -> get('user') -> result();
		foreach ($results as $result) {
			$userResult[] = $result;
			$resultIds[] = $result -> user_id;
		}
		
		$this->db->like('user_lname', $userString);
		$this -> db -> where('user_type !=', 'admin');
		if (count($resultIds) > 0) {
			$this -> db -> where_not_in('user_id', $resultIds);
		}
		$results = $this -> db -> get('user') -> result();
		foreach ($results as $result) {
			$userResult[] = $result;
			$resultIds[] = $result -> user_id;
		}
		
	return $userResult;
	}
	
	function getUserByUsername($username){
		//input all user data from database
		$users = $this->db->get_where('user', array('user_username' => $username))->result();
		return $users[0];
	}
	
	function getUserProfile($userID)
	{
		$users = $this->db->get_where('user', array('user_id' => $userID))->result();
		return $users[0];
	
	}
	function update_password($username, $password)
	{
		//change password from forgot password
		$query_str = "SELECT user_id from user where user_username =?";
		$result = $this->db->query($query_str, $username);
		
		$sha1_password = sha1($password);
		
		if($result->num_rows() == 1)
		{
			$query_str = "UPDATE user SET user_password = ? WHERE user_username = ?";
			$result = $this->db->query($query_str, array($sha1_password, $username));
			
			return true;
		}
		else
		{
			return false;
		}
		
	}
	
	function update_profile($data, $username){
		//update profile
		$this->db->where('user_username', $username);
		$this->db->update('user', $data); 
		
		
		
	}
	
	function change_password($username, $oldpassword,$newpassword){
		// change password with checking old password
		$sha1_oldpassword = sha1($oldpassword);
		$sha1_newpassword = sha1($newpassword);
		
		$query_str = "SELECT * FROM user WHERE user_username = ? and user_password = ?";
		
		$result = $this->db->query($query_str, array($username, $sha1_oldpassword))->result();
		
		if (count($result) == 1)
		{
			$query_str = "UPDATE user SET user_password = ? WHERE user_username = ?";
			$result = $this->db->query($query_str, array($sha1_newpassword, $username));
			return 0;
		}
		else 
		{
			//wrong password 
			return 1;
		}
		
	}
	
}

?>